package com.nbpt;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    EditText edtUsername,edtPassword;
    String resp;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtUsername = findViewById(R.id.editUsername);
        edtUsername.setText("test");

        edtPassword = findViewById(R.id.editPassword);
        edtPassword.setText("123456");
        sp = getSharedPreferences("token",Context.MODE_PRIVATE);

    }

    public void clickLogin(View view){
        String username = edtUsername.getText().toString();
        String password = edtPassword.getText().toString();


        if(edtUsername.getText().toString().isEmpty()||
                edtPassword.getText().toString().isEmpty() ){
            Toast.makeText(MainActivity.this,"登录失败！",Toast.LENGTH_SHORT).show();
        }else{
            OkHttpClient client = new OkHttpClient();
            MediaType JSON_TYPE = MediaType.parse("application/json;charset=utf-8");
            JSONObject upload=new JSONObject();
            try{
                upload.put("username",username);
                upload.put("password",password);
            }catch (JSONException e){
                e.printStackTrace();
            }
            String json = upload.toString();
            RequestBody body = RequestBody.create(JSON_TYPE,json);
            Request request = new Request.Builder()
                    .url("http://192.168.1.7:9020/iot/user/login")
                    .post(body).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //失败
                    Log.e("MainActivity",e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //成功
                    resp = response.body().string();
                    Log.e("MainActivity",resp);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject(resp);
                                int code = jsonObject.getInt("code");
                                String message = jsonObject.getString("msg");

                                if(code == 10001)
                                {

                                    Intent intent = new Intent(MainActivity.this,MenuActivity.class);
                                    startActivity(intent);
                                    JSONObject object = jsonObject.getJSONObject("data");
                                    String token = object.getString("token");
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("token",token);
                                    editor.commit();//保存
                                    Toast.makeText(MainActivity.this,"登录成功！"+token,
                                            Toast.LENGTH_SHORT).show();

                                }
                                else{
                                    Toast.makeText(MainActivity.this,message,
                                            Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });
        }

//        if(username.equals("admin") & password.equals("123")) {
//            Toast.makeText(MainActivity.this, "登陆成功！", Toast.LENGTH_SHORT).show();
//            Intent intent = new Intent(MainActivity.this, MenuActivity.class);
//            startActivity(intent);
//        }
//        else
//                    Toast.makeText(MainActivity.this,"登陆失败！",Toast.LENGTH_SHORT).show();
    }

//        if(username.equals("test") & Password.equals("123456"))
//           {
//             Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//             Intent intent = new Intent(MainActivity.this,MenuActivity.class);
//             startActivity(intent);
//           }
//        else
//            Toast.makeText(MainActivity.this,"登录失败" ,Toast.LENGTH_SHORT).show();
//    }

    public void onclick(View view){

        edtUsername = findViewById(R.id.editUsername);
        edtUsername.setText("");

        edtPassword = findViewById(R.id.editPassword);
        edtPassword.setText("");
    }

    public void clickRegister(View view){

        Toast.makeText(MainActivity.this,"点击了新用户注册" ,Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
        startActivity(intent);
    }
}
